# Integrate pgroll into your project

`pgroll` maintains multiple versions of the database schema side-by-side. For more details see the [Concepts page](concepts).

## Client applications

In order to work with the multiple versioned schema that `pgroll` creates, clients need to be configured to work with one of them.

This is done by having client applications configure the [search path](https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH) when they connect to the Postgres database.

### Examples

For example, this fragment for a Go client application shows how to set the `search_path` after a connection is established:

```go
db, err := sql.Open("postgres", "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable")
if err != nil {
    return nil, err
}

searchPath := "public_02_add_assignee_column"
log.Printf("Setting search path to %q", searchPath)
_, err = db.Exec(fmt.Sprintf("SET search_path = %s", pq.QuoteIdentifier(searchPath)))
if err != nil {
    return nil, fmt.Errorf("failed to set search path: %s", err)
}
```

In practice, the `searchPath` variable would be provided to the application as an environment variable.

Set `search_path` in Python using `psycopg2` with the [`Connection` class](https://www.psycopg.org/psycopg3/docs/api/connections.html#psycopg.Connection):

```python
import psycopg2

with psycopg2.connect('dbname=postgres user=postgres password=postgres options=-c search_path=public_02_add_assignee_column') as conn:
    pass
```

Another example for setting `search_path` in Node.js:

```js
const { Client } = require("pg");

const client = new Client({
  user: "postgres",
  host: "localhost",
  database: "postgres",
  password: "postgres",
  port: 5432,
});

client.connect();

client.query("SET search_path TO public_02_add_assignee_column", (err, res) => {
  if (err) {
    console.error("Error setting search_path", err.stack);
  } else {
    console.log("search_path set successfully");
    // Perform your database operations here
  }

  // End the connection
  client.end();
});
```

## CI/CD pipelines

`pgroll` can tell you the name of the latest schema version, either in the target database or local directory using the `latest schema` command.

```
$ pgroll latest schema
public_02_add_assignee_column
```

You can pass the `search_path` value to your application in your CI/CD pipeline, so your application can be tested against the latest database changes.

## What happens if the `search_path` is not set?

If an application doesn't set the `search_path` for the connection, the `search_path` defaults to the `public` schema, meaning that the application will be working with the underlying tables directly rather than accessing them through the versioned views.
